package tx.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tx.dao.PhoneDao;
import tx.service.PhoneService;

/**
 * @author ddy
 * @create 2020-08-17 09:44:06
 */
@Service
public class PhoneServiceImpl implements PhoneService {

    @Autowired
    private PhoneDao phoneDao;

    @Transactional()
    //@Transactional(noRollbackFor = BadSqlGrammarException.class)   //不回滚
    //根据手机的id购买手机
    public void purchase(String userName, String id) {
        //1.根据id查询手机的价格
        int price = phoneDao.findPriceById(id);

        //2.根据用户名和价格更新用户余额
        phoneDao.updateBalance(userName, price);

        //3.根据id更新手机的库存
        phoneDao.updateStock(id);
    }
}
